package com.jn.erp.web.api.student.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jn.erp.web.api.student.entity.StudentCourse;
import com.jn.erp.web.api.student.entity.TStudentCourse;
import com.jn.erp.web.api.student.entity.TStudentCourseSale;
import com.jn.erp.web.api.student.entity.vo.TStudentCourseVo;
import com.jn.erp.web.api.student.mapper.StudentCourseMapper;
import com.jn.erp.web.api.student.mapper.TStudentCourseSaleMapper;
import com.jn.erp.web.api.student.service.TStudentCourseSaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wyp
 * @since 2024-09-29
 */
@Service
public class TStudentCourseSaleServiceImpl extends ServiceImpl<TStudentCourseSaleMapper, TStudentCourseSale> implements TStudentCourseSaleService {
    @Autowired
    private StudentCourseMapper studentCourseMapper;
    @Override
    public String StudentCourseSaleUpdate(TStudentCourseVo tStudentCourseVo) {
        StudentCourse studentCourse = studentCourseMapper.selectOne(new QueryWrapper<StudentCourse>().eq("order_no", tStudentCourseVo.getOrderNo()));
        TStudentCourseSale tStudentCourseSale = new TStudentCourseSale();
        tStudentCourseSale.setEditor(tStudentCourseVo.getEditor());
        tStudentCourseSale.setEditTime(LocalDateTime.now());
        tStudentCourseSale.setDeleted("1");
        baseMapper.update(tStudentCourseSale,new QueryWrapper<TStudentCourseSale>().eq("deleted","0").eq("order_no",tStudentCourseVo.getOrderNo()));
        List<TStudentCourseSale> tStudentCourseSales = tStudentCourseVo.getTStudentCourseSales();
        List<TStudentCourseSale> tsAdd =tStudentCourseSales.stream().filter(u-> Objects.isNull(u.getId())).collect(Collectors.toList());
        List<TStudentCourseSale> tsUpdate =tStudentCourseSales.stream().filter(u-> !Objects.isNull(u.getId())).collect(Collectors.toList());
       if(!CollectionUtils.isEmpty(tsAdd)){
           tsAdd.stream().forEach(u->{
               u.setOrgId(tStudentCourseVo.getOrgId());
               u.setType(studentCourse.getType());
               u.setOrderNo(tStudentCourseVo.getOrderNo());
               u.setCreator(tStudentCourseVo.getEditor());
               u.setAddTime(LocalDateTime.now());
               u.setHandlingTime(tStudentCourseVo.getHandlingTime());
           });
           this.saveBatch(tsAdd);
       }
        if(!CollectionUtils.isEmpty(tsUpdate)){
            tsUpdate.stream().forEach(u->{
                u.setEditor(tStudentCourseVo.getEditor());
                u.setEditTime(LocalDateTime.now());
                u.setDeleted("0");
            });
            this.updateBatchById(tsUpdate);
        }

        return null;
    }
}
